package com.ibeeking.found.common.config.http;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

/**
 * @Author ibeeking
 */
@Configuration
@AutoConfigureAfter({RestTemplateAutoConfiguration.class,RestTemplateBuilder.class})
public class RestTemplateConfig {

    @Value("${resttemplate.okhttp.connectTimeOut:5}")
    private int connectTimeOut;

    @Value("${resttemplate.okhttp.readTimeout:5}")
    private int readTimeout;

    @Bean
    @Lazy
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder
                .requestFactory(OkHttp3ClientHttpRequestFactory.class)
                .setConnectTimeout(Duration.ofSeconds(connectTimeOut))
                .setReadTimeout(Duration.ofSeconds(readTimeout))
                .build();
    }

}
